package drawingManagement;

import loginTest.Login;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

import javax.naming.ldap.Control;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;
/*
        图纸的增删改查
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DrawingManagement  extends Login {
    @Override
    @ParameterizedTest
    @CsvFileSource(resources = "../login.csv")
    @Order(1)
    public void login(String url, String username, String password, String newUrl) throws InterruptedException {
        super.login(url, username, password, newUrl);
    }
    /*
        新增一个图纸
     */
    @ParameterizedTest
    @CsvSource("自动化测试,C:\\Users\\admin\\Desktop\\资源平台测试数据\\dwg图纸\\B1.dwg")
    @Order(2)
    void addDrawing(String name,String file) throws InterruptedException {
        //打开左侧栏
//        webDriver.findElement(By.cssSelector("#hamburger-container")).click();
        //选择图纸管理
        webDriver.findElement(By.cssSelector("#app > div > div.has-logo.sidebar-container > div.el-scrollbar.theme-dark > div.scrollbar-wrapper.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > ul > div:nth-child(2) > li > div")).click();
        sleep(1000);
        webDriver.findElement(By.cssSelector("#app > div > div.has-logo.sidebar-container > div.el-scrollbar.theme-dark > div.scrollbar-wrapper.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > ul > div:nth-child(2) > li > ul > div:nth-child(1)"))
                .click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
        //点击 新建
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div[2]/section/div/div/div[1]/div[1]/button[1]")).click();
        sleep(3000);
        //输入图纸名
        webDriver.findElement(By.cssSelector(".el-dialog__body .el-form-item__content .el-input__wrapper .el-input__inner")).sendKeys(name);
        //定位专业下拉框
        List<WebElement> elements =  webDriver.findElements(By.cssSelector(".el-overlay-dialog .el-form-item__content .el-input__wrapper .el-input__inner"));
        elements.get(1).click();
        //选择机械专业
        sleep(1000);
        webDriver.findElement(By.cssSelector("div > div > div.el-select-dropdown__wrap.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > ul > li:nth-child(1)")).click();
        //上传dwg图纸
        webDriver.findElement(By.cssSelector(" .el-upload  .el-upload__input[accept=\".dwg\"]")).sendKeys(file);
        sleep(3000);
        //点击保存
        webDriver.findElement(By.cssSelector(" div > div > footer > span > button.el-button.el-button--primary.el-button--default")).click();
        //body > div:nth-child(44) > div > div > footer > span > button.el-button.el-button--primary.el-button--default
        sleep(3000);
    }
    /*
        查询新增的图纸
     */
    @ParameterizedTest
    @CsvSource("自动化测试")
    @Order(3)
    void search(String name) throws InterruptedException {
        //输入图纸名称
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div[2]/section/div/div/form/div[2]/div/div/div/div/input")).sendKeys(name);
        sleep(1000);
        //点击查询
        webDriver.findElement(By.cssSelector("#app > div > div.hasTagsView.main-container > section > div > div > form > div.panel-item.search-btn > button:nth-child(1)")).click();
        sleep(1000);
        String ans = webDriver.findElement(By.cssSelector("#app > div > div.hasTagsView.main-container > section > div > div > div.table-area > div.pagination-container > div > span.el-pagination__total.is-first")).getText();
        System.out.println("查询到:"+ans+"数据");
        Assertions.assertNotEquals("共 0 条",ans);
    }
    @ParameterizedTest
    @CsvSource("自动化测试modify,C:\\Users\\admin\\Desktop\\资源平台测试数据\\dwg图纸\\B2.dwg,C:\\Users\\admin\\Desktop\\资源平台测试数据\\svgz\\建施13.svgz")
    @Order(4)
    void modify(String name,String file1,String file2) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        sleep(3000);
        //点击修改
        webDriver.findElement(By.cssSelector(".cell .el-icon")).click();
        sleep(3000);
        //输入图纸名
        webDriver.findElement(By.cssSelector(".el-dialog__body .el-form-item__content .el-input__wrapper .el-input__inner")).sendKeys(Keys.CONTROL,"A");
        webDriver.findElement(By.cssSelector(".el-dialog__body .el-form-item__content .el-input__wrapper .el-input__inner")).sendKeys(name);
       //删除已上传的dwg图纸
        webDriver.findElement(By.cssSelector(".right .el-button")).click();
        //上传dwg图纸
        sleep(1000);
        webDriver.findElement(By.cssSelector(" .el-upload  .el-upload__input[accept=\".dwg\"]")).sendKeys(file1);
        //上传svgz图纸
        sleep(1000);
        webDriver.findElement(By.cssSelector(".el-upload  .el-upload__input[accept=\".svgz\"]")).sendKeys(file2);
        //点击保存
        webDriver.findElement(By.cssSelector(" div > div > footer > span > button.el-button.el-button--primary.el-button--default")).click();
    }
    @ParameterizedTest
    @CsvSource("自动化测试modify")
    @Order(5)
    void delete(String name) throws InterruptedException {
        //删除匹配的所有图纸
        //勾选上所有图纸
        sleep(3000);
       webDriver.findElement(By.xpath("/html/body/div[1]/div/div[2]/section/div/div/div[2]/div[1]/div[1]/div[2]/table/thead/tr/th[1]/div/label")).click();
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div[2]/section/div/div/div[1]/div[1]/button[2]")).click();
        //#app > div > div.hasTagsView.main-container > section > div > div > div.table-area > div.el-table--fit.el-table--enable-row-transition.el-table--default.el-table.el-table--layout-fixed.is-scrolling-none > div.el-table__inner-wrapper > div.el-table__body-wrapper > div > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > table > tbody > tr:nth-child(1) > td.el-table_2_column_18.el-table-fixed-column--right.is-first-column.el-table__cell > div > button:nth-child(2)
        sleep(3000);
        //点击 确定删除
        webDriver.findElement(By.cssSelector("body > div.el-overlay.is-message-box > div > div > div.el-message-box__btns > button.el-button.el-button--primary.el-button--default")).click();
        sleep(3000);
        //重置查询条件
        webDriver.findElement(By.cssSelector("#app > div > div.hasTagsView.main-container > section > div > div > form > div.panel-item.search-btn > button:nth-child(2)")).click();
        sleep(1000);
        //输入图纸名称
        webDriver.findElement(By.xpath("/html/body/div[1]/div/div[2]/section/div/div/form/div[2]/div/div/div/div/input")).sendKeys(name);
        sleep(1000);
        //点击查询
        webDriver.findElement(By.cssSelector("#app > div > div.hasTagsView.main-container > section > div > div > form > div.panel-item.search-btn > button:nth-child(1)")).click();
        sleep(1000);
        //验证删除成功
        String str =webDriver.findElement(By.cssSelector("#app > div > div.hasTagsView.main-container > section > div > div > div.table-area > div > div.el-table__inner-wrapper > div.el-table__body-wrapper > div > div.el-scrollbar__wrap.el-scrollbar__wrap--hidden-default > div > div > span")).getText();
        System.out.println("查询结果:"+str);
        Assertions.assertEquals("暂无数据",str);
    }
}
